//
//  SearchViewController.m
//  LYouSubscribeApp
//
//  Created by grx on 2018/12/12.
//  Copyright © 2018年 grx. All rights reserved.
//

#import "LYouSearchViewController.h"
#import "LYouSearchListController.h"
#import "XYYSegmentControl.h"
#import "SearchTitleModel.h"

@interface LYouSearchViewController ()<XYYSegmentControlDelegate>

@property (nonatomic, strong) XYYSegmentControl *segmentView;
@property (nonatomic,strong)  NSMutableArray               *itemArray;
@property (nonatomic,strong)  NSMutableArray               *idArray;
@property(nonatomic,strong) UIButton *backButton;

@end

@implementation LYouSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorWithHexRGB(0x1F1E23);
    self.itemArray = [NSMutableArray arrayWithCapacity:0];
    self.idArray = [NSMutableArray arrayWithCapacity:0];
    /** 获取类别 */
    [self requestCategoryList];
}

-(void)requestCategoryList{
    NSString *url = [NSString stringWithFormat:@"%@/list",LYBASEURL];
    [[LYouNetClient sharedInstance]NetRequestGETWithRequestURL:url WithParameter:nil WithReturnValeuBlock:^(NSURLSessionDataTask *task,id  _Nullable responseObject,NSArray *responseDict) {
        [self.itemArray removeAllObjects];
        [self.idArray removeAllObjects];
        NSMutableArray *allArray = [NSMutableArray new];
        /** 解析数组 */
        for (NSDictionary *dic in responseDict) {
            SearchTitleModel *model = [[SearchTitleModel alloc]init];
            model.createtime = dic[@"createtime"];
            model.id = dic[@"id"];
            model.name = dic[@"name"];
            model.pid = dic[@"pid"];
            [allArray addObject:model];
            [self.itemArray addObject:model.name];
            [self.idArray addObject:model.id];
        }
        /** 创建类别 */
        [self buildSegment];
    } WithErrorBlock:^(NSURLSessionDataTask *task, NSError *error) {
        
    } WithFailureBlock:^{
        
    }];
}

#pragma mark - 配置segment
-(void)buildSegment
{
    CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
    CGFloat hight = 24;
    if (safeBottem>0) {
        hight = 44;
    }
    self.segmentView = [[XYYSegmentControl alloc]initWithFrame:CGRectMake(0 , hight , SCREEN_WIDTH, SCREEN_HEIGHT) channelName:_itemArray source:self];
    [self.segmentView setUserInteractionEnabled:YES];
    //XYYSegmentControl代理
    self.segmentView.segmentControlDelegate = self;
    //注意：设置字体大小，颜色，的背景，并且tabSelectionIndicatorLocation设置成XYYSegmentedControlSelectionIndicatorLocationNone 实现木有被选中效果
    //设置tab 颜色(可选)
    self.segmentView.tabItemNormalColor = [UIColor lightGrayColor];
    //设置tab 被选中的颜色(可选)
    self.segmentView.tabItemSelectedColor = [UIColor whiteColor];
    //    //设置tab 背景颜色(可选)
    self.segmentView.tabItemNormalBackgroundColor = [UIColor colorWithWhite:0.f alpha:0.0];
    //设置tab字体大小(可选)
    self.segmentView.tabItemNormalFont  = 18;
    //被选中字体大小 (可选)
    self.segmentView.tabItemSelectedFont = 22;
    //设置tab 被选中标识的位置
    self.segmentView.tabSelectionIndicatorLocation = XYYSegmentedControlSelectionIndicatorLocationNone;
    [self.view addSubview:self.segmentView];
    /** 返回 */
    [self.view addSubview:self.backButton];
    [self.backButton setFtCornerdious:35/2 Corners:UIRectCornerTopRight|UIRectCornerBottomRight];
}

-(NSUInteger)numberOfTab:(XYYSegmentControl *)view
{
    return [self.itemArray count];//items决定
}

///待加载的控制器
-(UIViewController *)slideSwitchView:(XYYSegmentControl *)view viewOfTab:(NSUInteger)number
{
    LYouSearchListController *listVC = [[LYouSearchListController alloc] init];
    return listVC;
}

- (void)slideSwitchView:(XYYSegmentControl *)view didselectTab:(NSUInteger)number
{
    LYouSearchListController *listVC = view.viewArray[number];
    NSString *categoryId = [NSString stringWithFormat:@"%@",self.idArray[number]];
    [listVC requestData:categoryId];
    listVC.view.backgroundColor = [UIColor whiteColor];
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton new];
        CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
        CGFloat hight = 28;
        if (safeBottem>0) {
            hight = 48;
        }
        self.backButton.frame = CGRectMake(0, hight, 45, 35);
        [_backButton setImage:[UIImage imageNamed:@"backLeftBg"] forState:UIControlStateNormal];
        [_backButton setImage:[UIImage imageNamed:@"backLeftBg"] forState:UIControlStateHighlighted];
        [_backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

-(void)backButtonClick:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
